<?php

/*
 * Helper/Day.php
 */

namespace RHX\Helper;

use RHX\Model\Object\PlanningSemaine;
use RHX\Model\Object\XRules;
use RHX\Helper\RHDateTime;
use RHX\Helper\Number;

class Day {

    /**
     *
     * @var int
     */
    public $matricule;

    /**
     *
     * @var int
     */
    public $code;

    /**
     *
     * @var string
     */
    public $date;

    /**
     *
     * @var string
     */
    public $dayName;

    /**
     *
     * @var string
     */
    public $entree;

    /**
     *
     * @var string
     */
    public $sortie;

    /**
     *
     * @var boolean
     */
    public $pause;

    /**
     *
     * @var boolean
     */
    public $repas;

    /**
     *
     * @var string
     */
    public $entreeRef;

    /**
     *
     * @var string
     */
    public $sortieRef;

    /**
     *
     * @var boolean
     */
    public $retard;

    /**
     *
     * @var boolean
     */
    public $anticipe;

    /**
     *
     * @var float
     */
    public $coef;

    /**
     *
     * @var float
     */
    public $taux;

    /**
     *
     * @var float
     */
    public $hebdo;

    /**
     *
     * @var boolean
     */
    private $error		 = false;

    /**
     *
     * @var string
     */
    private $errorMessage	 = '';

    /**
     * Constructeur
     * 
     * @param \RHX\Model\Object\PlanningSemaine $planning
     * @param string $day
     */
    public function __construct($planning = null, $day = 'lundi') {
	if (!is_null($planning)) {
	    try {
		$this->fill($planning, $day);
	    }
	    catch (Exception $ex) {
		$this->error		 = true;
		$this->errorMessage	 = $ex->getMessage();
	    }
	}
	$this->dayName = $day;
    }

    /**
     * Remplissage des données
     * 
     * @param \RHX\Model\Object\PlanningSemaine $planning
     * @param string $day
     */
    public function fill(PlanningSemaine $planning, $day) {
	try {
	    $dayCode	 = $day . '_code';
	    $dayEntree	 = $day . '_entree';
	    $daySortie	 = $day . '_sortie';
	    $dayPause	 = $day . '_pause';
	    $dayRepas	 = $day . '_repas';
	    $dayEntreeRef	 = $day . '_entree_ref';
	    $daySortieRef	 = $day . '_sortie_ref';
	    $this->matricule = (int) $planning->matricule;
	    $this->code	 = (int) $planning->$dayCode;
	    $this->date	 = $planning->$day;
	    $this->dayName	 = $day;
	    $this->entree	 = RHDateTime::hourNoSecond($planning->$dayEntree);
	    $this->sortie	 = RHDateTime::hourNoSecond($planning->$daySortie);
	    $this->entreeRef = RHDateTime::hourNoSecond($planning->$dayEntreeRef);
	    $this->sortieRef = RHDateTime::hourNoSecond($planning->$daySortieRef);
	    $this->pause	 = ($planning->$dayPause == 1) ? true : false;
	    $this->repas	 = ($planning->$dayRepas == 1) ? true : false;
	    $refEntree	 = (is_null($planning->$dayEntreeRef)) ? '00:00' : RHDateTime::hourNoSecond($planning->$dayEntreeRef);
	    $refSortie	 = (is_null($planning->$daySortieRef)) ? '00:00' : RHDateTime::hourNoSecond($planning->$daySortieRef);
	    $this->retard	 = $this->isLate($this->code, $planning->$dayEntree, $refEntree);
	    $this->anticipe	 = $this->isAnticipe($this->code, $planning->$daySortie, $refSortie);
	    $this->coef	 = floatval($planning->coef);
	    $this->taux	 = floatval($planning->taux);
	    $this->hebdo	 = floatval($planning->hebdo);
	}
	catch (\Exception $ex) {
	    throw $ex;
	}
    }

    /**
     * Indique si il y a retard
     * 
     * @param int $code
     * @param string $entree
     * @param string $refEntree
     * @return boolean
     */
    private function isLate($code, $entree, $refEntree) {
	if ($refEntree == '00:00')
	    return false;
	if ($code == XRules::ProblemeRetardDepartAnticipe) {
	    $in	 = new RHDateTime();
	    $in->setTime($entree);
	    $ref	 = new RHDateTime();
	    $ref->setTime($refEntree);
	    if ($in->isGT($ref)) {
		return true;
	    }
	}
	return false;
    }

    /**
     * 
     * @param int $code
     * @param string $sortie
     * @param string $refSortie
     * @return boolean
     */
    private function isAnticipe($code, $sortie, $refSortie) {
	if ($refSortie == '00:00')
	    return false;
	if ($code == XRules::ProblemeRetardDepartAnticipe) {
	    $out	 = new RHDateTime();
	    $out->setTime($sortie);
	    $ref	 = new RHDateTime();
	    $ref->setTime($refSortie);
	    if ($out->isLT($ref)) {
		return true;
	    }
	}
	return false;
    }

    /**
     * Récupérer la différence entre l'entrée et la sortie
     * 
     * @param boolean $decimal
     * @param boolean $print
     * @return float|string
     */
    public function getDiff($decimal = true, $print = false) {
	$diff = RHDateTime::diffHours($this->sortie, $this->entree);
	if ($this->pause) {
	    $diff = RHDateTime::diffHours($diff, '00:30');
	}
	$value = $diff;
	if ($decimal) {
	    $value = RHDateTime::toFloat($diff);
	    if ($print) {
		$value = Number::frenchFormat($value);
	    }
	}
	return $value;
    }

    /**
     * Valeur des heures de la journée
     * 
     * @param boolean $print
     * @return float|string
     */
    public function getValue($coef = null, $taux = null, $print = false) {
	if ($this->isCountable()) {
	    $diff	 = $this->getDiff(true);
	    $coef	 = is_null($coef) ? $this->coef : floatval($coef);
	    $taux	 = is_null($taux) ? $this->taux : floatval($taux);
	    $value	 = $diff * $coef * $taux;
	    if ($this->code == XRules::Ferie2) {
		$value = $value * 2;
	    }
	    return ($print) ? Number::frenchFormat($value) : $value;
	}
	return ($print) ? '0,00' : 0.0;
    }

    /**
     * Récupérer les heures de retard
     * 
     * @param boolean $decimal
     * @param boolean $print
     * @return float|string
     */
    public function getRetard($decimal = true, $print = false) {
	$retard = '00:00';
	if ($this->retard != false || $this->entreeRef != '00:00') {
	    $retard = RHDateTime::diffHours($this->entree, $this->entreeRef);
	}
	if ($decimal) {
	    $retard = RHDateTime::toFloat($retard);
	    if ($print) {
		$retard = Number::frenchFormat($retard);
	    }
	}
	return $retard;
    }

    /**
     * Récupérer les heures de départ anticipé
     * 
     * @param boolean $decimal
     * @param boolean $print
     * @return float|string
     */
    public function getAnticipe($decimal = true, $print = false) {
	$anticipe = '00:00';
	if ($this->anticipe != false || $this->sortieRef != '00:00') {
	    $anticipe = RHDateTime::diffHours($this->sortieRef, $this->sortie);
	}
	if ($decimal) {
	    $anticipe = RHDateTime::toFloat($anticipe);
	    if ($print) {
		$anticipe = Number::frenchFormat($anticipe);
	    }
	}
	return $anticipe;
    }

    /**
     * Détermine si on comptabilise ou pas
     * 
     * @return boolean
     */
    private function isCountable() {
	if ($this->code == XRules::Present || $this->code == XRules::CongePaye || $this->code == XRules::TransfertEntrant || $this->code == XRules::TransfertSortant || $this->code == XRules::ArretMaladie2 || $this->code == XRules::AccidentTravail || $this->code == XRules::Ferie1 || $this->code == XRules::Ferie2 || $this->code == XRules::Ferie3 || $this->code == XRules::CongeMaternite || $this->code == XRules::CongeMaternitePere || $this->code == XRules::CongePayeExceptionnel || $this->code == XRules::ProblemeRetardDepartAnticipe || $this->code == XRules::EcoleFormationIntegration || $this->code == XRules::EcoleFormationEmploye || $this->code == XRules::EcoleFormationAlternance) {
	    return true;
	}
	return false;
    }

    /**
     * Heure d'entrée
     * 
     * @return \RHX\Helper\RHDateTime
     */
    public function getEntree() {
	$entree = new RHDateTime($this->date);
	$entree->setTime($this->entree);

	return $entree;
    }

    /**
     * Heure de sortie
     * 
     * @return \RHX\Helper\RHDateTime
     */
    public function getSortie() {
	$sortie = new RHDateTime($this->date);
	$sortie->setTime($this->sortie);

	return $sortie;
    }

}
